<?php
	
	class Application extends Controller {
	
		function Application()
		{
			parent::Controller();	
		}
		
		function sandboxnew()
		{
			$this->db->order_by( 'id' );
			$result = $this->db->get( 'entries' );

			$vars['result'] = $result;
			$this->load->view( 'sandbox_new', $vars );
		}
		
		function sandbox()
		{
			$this->db->order_by( 'id' );
			$result = $this->db->get( 'entries' );
			
			$dayArray = array();
			foreach ($result->result() as $row)
			{
				$dayArray[ $row->start ] = 'start';
				$dayArray[ $row->end ] = 'end';
			}
			
			ksort( $dayArray );
				
			$counter = 0;
			$nearArray = array();
			foreach ($dayArray as $key => $val)
			{
				switch( $val )
				{
					case 'start':
						$counter++;
						break;
					case 'end':
						$counter--;
						break;
				}
				$nearArray[ $key ] = $counter;
			}
			
			$vars['content'] = $this->createDay( $result, $nearArray );
			
			$this->load->view( 'sandbox', $vars );
		}
		
		function index()
		{
			$this->db->order_by( 'id' );
			$result = $this->db->get( 'entries' );
			
			$dayArray = array();
			foreach ($result->result() as $row)
			{
				$dayArray[ $row->start ] = 'start';
				$dayArray[ $row->end ] = 'end';
			}
			
			ksort( $dayArray );
				
			$counter = 0;
			$nearArray = array();
			foreach ($dayArray as $key => $val)
			{
				switch( $val )
				{
					case 'start':
						$counter++;
						break;
					case 'end':
						$counter--;
						break;
				}
				
				//echo date( "H:i", $key ) . ' (' . $val . ') [' . $counter . ']<br/>';
				$nearArray[ $key ] = $counter;
			}
			
			//$vars['result'] = $result;
			//$vars['nearArray'] = $nearArray;
			
			$vars['content'] = $this->createDay( $result, $nearArray );
			
			/*
			for ($i = 0; $i < 50; $i++) {

				$data = array(
				   'title' => 'New Event ' . $i
				);

				$this->db->insert('entries', $data);

				echo $this->db->insert_id() . ', ';
			
			}
			*/
			
			$this->load->view( 'calendar_test', $vars );
		}
		
		function createDay( $result, $nearArray )
		
		{
			
			/* TODO
			 * 
			 * 	- click soll kein update mehr machen, nur noch beim resizen
			 * 	- delele funktion bei klick auf 'x'
			 *	- add soll request machen
			 *	- mox werte bei neuerstellten events
			 *  
			 * 
			 */
			
			
			$hourHeight = 25;
			$dayWidth = 400;
			
			$return = '<div id="day" style="height: ' . 24 * $hourHeight . 'px; width: ' . $dayWidth . '">';
			
			$return .= '<table height="' . 24 * $hourHeight . '" width="' . $dayWidth . '">';
			
				for ($i = 0; $i < 24; $i++) {
				
					$return .= '<tr><td>&nbsp;</td></tr>';
				
				}
				
				$return .= '</table>';
			
				$lastPartner = 1;
				foreach ( $result->result_array() as $row )
				{
					$partners = 0;
					foreach ($nearArray as $key => $val)
					{
						if( $key >= $row['start'] and $key <= $row['end'] )
						{
							if( $val > $partners ) $partners = $val;
						}
					}
					
					if( $lastPartner == $nearArray[ $row['start'] ] && $lastPartner == 0 )
					{
						$currPartners = 1;
						$left = 0;
					}
						else
					{
						$currPartners = $partners;
						$left = ( ( $nearArray[ $row['start'] ] - 1 ) / $currPartners ) * $dayWidth;
					}

					$start = ( date( "G", $row['start'] ) + ( ( date( "i", $row['start'] ) * (10/6) ) ) / 100 );
					$duration = ( date( "G", $row['end'] ) + ( ( date( "i", $row['end'] ) * (10/6) ) ) / 100 ) - $start;
			
					$style = 'left: ' . $left . 'px; width: ' . $dayWidth / $partners . 'px; height: ' . $duration * $hourHeight . 'px; top: ' . $start * $hourHeight . 'px;';
					$return .= '<div id="' . $row['id'] . '" class="entry" style="' . $style . '"><div class="entryHeader">' . date( "H:i", $row['start'] ) . '</div>' . $row[ 'title' ] . '<div class="delete" onclick="deleteEntry($(this.parentNode).id)"></div><div class="handler" id="handler' . $row['id'] . '"></div></div>';
					
					$return .= '<script type="text/javascript" charset="utf-8">';
						$maxHeight = ( 24 * $hourHeight ) - ( $start * $hourHeight );
						
						// dragging fuer objekt
						
						/*	
						
						$return .= "$('" . $row['id'] . "').makeResizable({
						
								onStart: function() {
								
									oldEventStart = $('" . $row['id'] . "').style.top;
								
								},
								
								onDrag: function() {
					
									updateEntryStart('" . $row['id'] . "');
						
								},
								 
								onComplete: function() { 
								
									newEventStart = $('" . $row['id'] . "').style.top;
										
									if ( oldEventStart !== newEventStart ) {
							
										editEntry('" . $row['id'] . "');
										
									} 
								
								}, 
								
								grid: " . $hourHeight / 2 .", 
								modifiers: {x: false, y: 'top'}, 
								limit: {y: [0, " . ( 24 * $hourHeight ) . "]} 
										
								});";
						
						*/
						
						// dragging mit handler
						
						/*
						$return .= "$('" . $row['id'] . "').makeResizable({ 
						
								onComplete: function() { 
								
										editEntry('" . $row['id'] . "');
								
								}, 
								
								handle: 'handler" . $row['id'] . "', grid: " . $hourHeight / 2 .", 
								modifiers: {x: false, y: 'height'}, 
								limit: {y: [" . $hourHeight . ", " . $maxHeight . "]} 
										
								});";
						
						*/
								
					$return .= '</script>';
					
					// onStart: function() { saveStart( " . $row['id'] . " ); }, onComplete: function() { saveEntry( " . $row['id'] . ", " . $row['start'] . ", " . $row['end'] . " ); }, 
					
					$lastPartner = $nearArray[ $row['start'] ];
				}
				
							
			$return .= '</div>';
			
			return $return;
		}
		
		function saveEntry() {
			
			if( $_POST['oldStart'] != '' )
			{
				$start = $_POST['start'] + ( ( $_POST['newStart'] - $_POST['oldStart'] ) * 3600 );
			}
				else
			{
				$start = $_POST['start'];
			}
			
			if( isset( $_POST['newLength'] ) )
			{
				$end = $start + ( $_POST['newLength'] * 3600 );
			}
				else
			{
				$end = $_POST['end'];
			}
			

			
			$data = array(
               'start' => $start,
               'end' => $end
            );

			$this->db->where('id', $_POST['id']);
			$this->db->update('entries', $data); 
			
			redirect('');
		}
		
		function addEntry() {
		
			$hour = 25;
			// wieso wird die nicht uebernommen????
		
			// px -> timestamp
			$start 	= 1199142000 + (($_POST['start'] / $hour) * 3600);
			$end 	= $start + 14400;
			
			$data = array(
               'start' 	=> 	$start,
               'end' 	=> 	$end,
               'title'	=>	'Neues Ereignis'
            );

			$this->db->insert('entries', $data);
			
			// timestamp -> px
			$start	= (($start - 1199142000) / 3600) * $hour;
			$end	= (($end - 1199142000) / 3600) * $hour;
			
			echo $this->db->insert_id() .',' . $start .',' . $end; 
			
		}
		
		function editEntry() {
		
			$data = array(
               'start' 	=> 	$_POST['start'],
               'end'	=>	$_POST['end']
            );
            
            $this->db->where('id', $_POST['id']);
			$this->db->update('entries', $data); 
		
		
		
		}
		
		function deleteEntry() {
		
			$this->db->delete('entries', array('id' => $_POST['id'])); 
		
		}
	}
?>